﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.Tools;
using bdnt_pets_system_Entity;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.DataManager
{
    public partial class PartManagerControl : BaseUserControl
    {
        DataServiceReference.DataServiceClient dataClient;
        int pageSize = SessionManager.PageSize;
        int currentPage = 1;

        public PartManagerControl()
        {
            InitializeComponent();
            dataClient = new DataServiceReference.DataServiceClient();
            InitEvent();
            QueryData();
        }

        void InitEvent()
        {
            btnNew.Click += new RoutedEventHandler(btnNew_Click);
            btnSelect.Click += new RoutedEventHandler(btnSelect_Click);
            dataClient.DeleteCuPartCompleted += new EventHandler<DataServiceReference.DeleteCuPartCompletedEventArgs>(dataClient_DeleteCuPartCompleted);
            MyPageControl.dpPage.PageIndexChanged += new EventHandler<EventArgs>(dpPage_PageIndexChanged);
            dataClient.GetPartListByConditionCountCompleted += new EventHandler<DataServiceReference.GetPartListByConditionCountCompletedEventArgs>(dataClient_GetPartListByConditionCountCompleted);
            dataClient.GetPartListByConditionCompleted += new EventHandler<DataServiceReference.GetPartListByConditionCompletedEventArgs>(dataClient_GetPartListByConditionCompleted);
        }
        Tbl_Part cuSelectObj = new Tbl_Part();
        #region 查询与分页
        void QueryData()
        {
            cuSelectObj.IsImportantSelect = rbFxAll.IsChecked.Value ? -1 : (rbFxYes.IsChecked.Value ? 1 : 0);
            dataClient.GetPartListByConditionCountAsync(cuSelectObj);
        }

        void dpPage_PageIndexChanged(object sender, EventArgs e)
        {
            currentPage = MyPageControl.PageIndex + 1;
            dataClient.GetPartListByConditionAsync(cuSelectObj, pageSize, currentPage);
        }

        void dataClient_GetPartListByConditionCompleted(object sender, DataServiceReference.GetPartListByConditionCompletedEventArgs e)
        {
            gdList.ItemsSource = e.Result;
        }

        void dataClient_GetPartListByConditionCountCompleted(object sender, DataServiceReference.GetPartListByConditionCountCompletedEventArgs e)
        {
            MyPageControl.LoadPages(e.Result, pageSize);
        }

        #endregion

        void btnSelect_Click(object sender, RoutedEventArgs e)
        {
            QueryData();
        }

        void btnNew_Click(object sender, RoutedEventArgs e)
        {
            var cuv = new NewPartWindow(new Tbl_Part());
            cuv.Show();
            cuv.Closed += new EventHandler(cuv_Closed);
        }

        void cuv_Closed(object sender, EventArgs e)
        {
            if ((sender as NewPartWindow).DialogResult == true)
                QueryData();
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            var cuObj = (sender as HyperlinkButton).Tag as Tbl_Part;
            var cupage = new NewPartWindow(cuObj);
            cupage.Show();
            cupage.Closed += new EventHandler(cuv_Closed);
        }

        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show(MyStrings.qdyscm,MyStrings.ts, MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                return;
            var cuObj = (sender as HyperlinkButton).Tag as Tbl_Part;
            dataClient.DeleteCuPartAsync(cuObj);
        }

        void dataClient_DeleteCuPartCompleted(object sender, DataServiceReference.DeleteCuPartCompletedEventArgs e)
        {
            if (e.Result)
                QueryData();
        }

    }
}
